package com.workcell.workdata.xthings.controller;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.workcell.workdata.common.core.util.R;
import com.workcell.workdata.common.excel.annotation.ResponseExcel;
import com.workcell.workdata.xthings.dto.BaseVo;
import com.workcell.workdata.xthings.entity.BaseValve;
import com.workcell.workdata.xthings.entity.Manufacturer;
import com.workcell.workdata.xthings.service.BaseValveService;
import com.workcell.workdata.xthings.service.ManufacturerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 阀门表
 *
 * @author pigx code generator
 * @date 2023-05-16 10:44:41
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/baseValve" )
@Api(value = "baseValve", tags = "阀门表管理")
public class BaseValveController {

    private final BaseValveService baseValveService;
    private final ManufacturerService manufacturerService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param baseValve 阀门表
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    public R getBaseValvePage(Page page, BaseValve baseValve) {
        return R.ok(baseValveService.page(page, Wrappers.query(baseValve)));
    }

    /**
     * 模糊分页查询
     * @param baseVo
     * @return
     */
    @ApiOperation(value = "模拟分页查询", notes = "模拟分页查询")
    @PostMapping("/vaguePage" )
    public R vaguePage(@RequestBody BaseVo baseVo) {
        return R.ok(baseValveService.vaguePage(baseVo));
    }

    /**
     * 通过id查询阀门表
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    public R getById(@PathVariable("id" ) String id) {
        return R.ok(baseValveService.getById(id));
    }

    /**
     * 新增阀门表
     * @param baseValve 阀门表
     * @return R
     */
    @ApiOperation(value = "新增阀门表", notes = "新增阀门表")
    @PostMapping
    public R save(@RequestBody BaseValve baseValve) {
		Manufacturer manufacturer = manufacturerService.selectByManufacturerCode(baseValve.getDeviceManufacturer());
		Assert.notNull(manufacturer, "厂商查询为空");
		baseValve.setManufacturerShortName(manufacturer.getManufacturerShortName());
		baseValve.setManufacturerFullName(manufacturer.getManufacturerFullName());
		baseValve.setManufacturer(manufacturer.getManufacturerFullName());
		baseValve.setManufacturerCode(manufacturer.getManufacturerCode());
        return R.ok(baseValveService.save(baseValve));
    }

    /**
     * 修改阀门表
     * @param baseValve 阀门表
     * @return R
     */
    @ApiOperation(value = "修改阀门表", notes = "修改阀门表")
    @PutMapping
    public R updateById(@RequestBody BaseValve baseValve) {
        Manufacturer manufacturer = manufacturerService.selectByManufacturerCode(baseValve.getDeviceManufacturer());
        Assert.notNull(manufacturer, "厂商查询为空");
        baseValve.setManufacturerShortName(manufacturer.getManufacturerShortName());
        baseValve.setManufacturerFullName(manufacturer.getManufacturerFullName());
        baseValve.setManufacturer(manufacturer.getManufacturerFullName());
		baseValve.setManufacturerCode(manufacturer.getManufacturerCode());
        return R.ok(baseValveService.updateById(baseValve));
    }

    /**
     * 通过id删除阀门表
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除阀门表", notes = "通过id删除阀门表")
    @DeleteMapping("/{id}" )
    public R removeById(@PathVariable String id) {
        return R.ok(baseValveService.removeById(id));
    }


    /**
     * 导出excel 表格
     * @param baseValve 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    public List<BaseValve> export(BaseValve baseValve) {
        return baseValveService.list(Wrappers.query(baseValve));
    }

    /**
     * 修改状态
     * @param baseValve
     * @return R
     */
    @ApiOperation(value = "修改阀门状态信息", notes = "修改阀门状态信息")
    @PutMapping("/updateStateById")
    public R updateStateById(@RequestBody BaseValve baseValve) {
        return R.ok(baseValveService.updateStateById(baseValve));
    }

}
